from lib.ta import MovingAverage

# macd(35,5,5) - moving average convergence / divergence
def MACD(object,periodEMA1,periodEMA2,periodEMA3,name1,name2,name3):
  if periodEMA1 <= periodEMA2 :
    tswap = periodEMA1
    periodEMA1 = periodEMA2
    periodEMA2 = tswap
  
  count = 1
  lastEMA1 = 0
  lastEMA2 = 0  
  lastEMA3 = 0 
  k1 = 2.0/(periodEMA1+1)
  k2 = 2.0/(periodEMA2+1)  
  k3 = 2.0/(periodEMA3+1)  
  for price in reversed(object) :
    if count < periodEMA1 :
      lastEMA1 = lastEMA1 + price.Close
    elif count == periodEMA1 :
      lastEMA1 = (lastEMA1 + price.Close)/periodEMA1
    else :
      lastEMA1 = price.Close*k1 + lastEMA1*(1-k1)
      
    if count < periodEMA2 :
      lastEMA2 = lastEMA2 + price.Close
    elif count == periodEMA2 :
      lastEMA2 = (lastEMA2 + price.Close)/periodEMA2
    else :
      lastEMA2 = price.Close*k2 + lastEMA2*(1-k2)
    
    
    if count >= periodEMA1 :
      macd = lastEMA2-lastEMA1
      price.__dict__[name1] = macd
      if count < periodEMA1+periodEMA3:
        lastEMA3 = lastEMA3 + macd
      elif count == periodEMA1+periodEMA3 :
        lastEMA3 = (lastEMA3 + macd)/periodEMA3
      else :
        lastEMA3 = macd*k3 + lastEMA3*(1-k3)
        price.__dict__[name2] = lastEMA3
        price.__dict__[name3] = macd - lastEMA3
      
    count = count + 1    
  return object

# adx(14) - average directional movement index 
def ADX(object,period,name):
      
  return

# rsi(close,13,30,70) - relative strength index
def RSI(object,period,name):
      
  return

# vad(14) - variable accumulation/distribution  
def VAD(object,period,name):
      
  return

# msto(14,3,3) - modified stochastics oscillator(14,3,3)  
def MSTO(object,period,name):
      
  return
